package com.burger.counter.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.burger.counter.beans.Burger;
import com.burger.counter.beans.Cheese;
import com.burger.counter.beans.Item;
import com.burger.counter.beans.ShoppingCart;

import com.burger.counter.business.CheeseBusiness;
import com.burger.counter.business.ItemBusiness;


/**
 * Servlet implementation class BServlet
 */
public class CheeseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// Getting data to starter Menu Option
		System.out.println("i am in Cheese servlet");
		CheeseBusiness burgerBI = new CheeseBusiness();
		ItemBusiness ItemBI = new ItemBusiness();
		Item item;
		String BurgerName="";
		Double BurgerPrice=0.0;
		Cheese cheeseList[] =null;
		Burger[] burgerList;
		ArrayList<Item> al;
		HttpSession session = request.getSession(true);
		
		int regionId =(Integer)session.getAttribute("RegionId");
		try {
			cheeseList = burgerBI.getCheeseDetails(regionId);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		int userId = (Integer)session.getAttribute("UserId");
		System.out.println("User Id in session.......:"+userId);
		Integer	BurgerId = Integer.parseInt(request.getParameter("BurgerId"));
	
		burgerList=(Burger[])session.getAttribute("burgerData");
		for(int i=0;i<burgerList.length;i++){
			if(BurgerId == burgerList[i].getBurgerId() ){
				BurgerName = burgerList[i].getBurgerName();
				BurgerPrice = burgerList[i].getBurgerPrice();
				
			}
		}
		
		//String BurgerName = (String) request.getParameter("BurgerName");
		//System.out.println("Burger selected..........:"+BurgerName);
		//Double BurgerPrice = Double.parseDouble(request.getParameter("BurgerPrice"));
		ShoppingCart cart=(ShoppingCart)session.getAttribute("Cart");
		item = ItemBI.setItemDetails(BurgerId,userId, BurgerName, BurgerPrice);
		if(cart == null){
			cart = new ShoppingCart();
			cart.addItemInCart(item);
			session.setAttribute("Cart", cart);
		}else{
			cart.addItemInCart(item);
			session.setAttribute("Cart", cart);
		}
		ShoppingCart cart1=(ShoppingCart)session.getAttribute("Cart");
		al = cart1.getItemInCart();
		
		for(int i=0;i<al.size();i++){
			
			System.out.println("Item Id"+al.get(i).getItemId());
			System.out.println("User Id"+al.get(i).getUserId());
			//System.out.println("Item Price"+al.get(i).getItemPrice());
			//System.out.println("Item Name"+al.get(i).getItemName());
		}			
		session.setAttribute("cheeseData", cheeseList);
		
//		for(int i=0;i<burgerList.length;i++)
//		{
//			System.out.println("ID:" + burgerList[i].getBurgerName());
//			System.out.println("Name:" + burgerList[i].getBurgerWeight());
//			
//		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/cheese.jsp");
		dispatcher.forward(request, response);
	}
}
